import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      component: () => import('../views/Login/index.vue')
    },
    {
      path: '/',
      component: () => import('../views/Home/index.vue'),
      children: [
        {
          path: '/',
          component: () => import('../views/Null/index.vue'),
        },
        {
          path: 'setting',
          component: () => import('../views/Setting/index.vue'),
          children: [
            {
              path: 'base',
              component: () => import('../views/Setting/base.vue')
            },
            {
              path: 'buy',
              component: () => import('../views/Setting/buy.vue')
            },
            {
              path: 'ship',
              component: () => import('../views/Setting/buy.vue')
            }
          ]
        },
        {
          path: 'distribution',
          component: () => import('../views/Distribution/indexOne.vue'),
          children: [
            {
              path: 'index',
              component: () => import('../views/Distribution/index.vue')
            },
            {
              path: 'setting',
              component: () => import('../views/Distribution/setting.vue')
            }
          ]
        },
        {
          path: 'goods',
          component: () => import('../views/Goods/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/Goods/list.vue')
            }
          ]
        },
        {
          path: 'category',
          component: () => import('../views/category/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/category/list.vue')
            }
          ]
        },
        {
          path: 'skus',
          component: () => import('../views/skus/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/skus/list.vue')
            }
          ]
        },
        {
          path: 'coupon',
          component: () => import('../views/coupon/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/coupon/list.vue')
            }
          ]
        },
        {
          path: 'user',
          component: () => import('../views/user/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/user/list.vue')
            }
          ]
        },
        {
          path: 'level',
          component: () => import('../views/level/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/level/list.vue')
            }
          ]
        },
        {
          path: 'manager',
          component: () => import('../views/manager/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/manager/list.vue')
            }
          ]
        },
        {
          path: 'access',
          component: () => import('../views/access/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/access/list.vue')
            }
          ]
        },
        {
          path: 'role',
          component: () => import('../views/role/list.vue'),
          children: [
            {
              path: 'list',
              component: () => import('../views/role/list.vue')
            }
          ]
        },
      ]
    },
  ]
})
// 路由全局前置守卫
router.beforeEach((to, from, next) => {
  // 验证 token 只有 token 存在的时候才能跳转到内容页面
  let token = localStorage.getItem("pinia-user")
  // console.log(token);
  if (token || to.path === '/login') {
    next()
  } else {
    // 判断有无登录
    // const userinfo = JSON.parse(localStorage.getItem('vuex'))
    if (!token) {
      next('/login')
    } else {
      next(true)
    }
    // next('/Login')
  }
})
export default router
